package org.software.dao;

import org.software.utils.DbUtil;
import org.software.entity.Class;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClassDao {
    private DbUtil dbUtil;


    public boolean classAdd(String className) {
        dbUtil = new DbUtil();

        Connection conn = dbUtil.getConnection();
        String sql = "insert into class(class_name) values(?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,className);//防止SQL注入
            return ps.executeUpdate()>0;
        }catch (Exception e) {
            e.printStackTrace();
            return false;//如果在执行过程中发生异常，捕获异常并打印堆栈跟踪，以便调试。同时返回 false 表示插入失败。
        }finally {
            dbUtil.close(conn);//关闭链接
        }
    }

    public boolean classDelete(String className) {
        dbUtil = new DbUtil();
        Connection conn = dbUtil.getConnection();
        String sql = "delete from class where class_name=?";
        try{
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,className);//防止SQL注入
            return ps.executeUpdate()>0;
        }catch (Exception e) {
            e.printStackTrace();
            return false;//如果在执行过程中发生异常，捕获异常并打印堆栈跟踪，以便调试。同时返回 false 表示插入失败。
        }finally {
            dbUtil.close(conn);//关闭链接
        }
    }

    public boolean classUpdate(String className,String newClassName) {
        dbUtil = new DbUtil();
        Connection conn = dbUtil.getConnection();
        String sql = "update class set class_name=? where class_name=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,newClassName);//防止SQL注入
            ps.setString(2,className);
            boolean result = ps.executeUpdate()>0;
            return result;
        }catch (Exception e) {
            e.printStackTrace();
            return false;//如果在执行过程中发生异常，捕获异常并打印堆栈跟踪，以便调试。同时返回 false 表示插入失败。
        }finally {
            dbUtil.close(conn);//关闭链接
        }
    }

    public List<Class> classQuery() {
        dbUtil = new DbUtil();
        Connection conn = dbUtil.getConnection();
        String sql = "select * from class";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            List<Class> list = new ArrayList<>();
            while (rs.next()) {
                Class classes = new Class();
                classes.setClassId(rs.getInt("class_id"));
                classes.setClassName(rs.getString("class_name"));
                classes.setGroupCount(rs.getInt("group_count"));
                list.add(classes);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            dbUtil.close(conn);//关闭链接
        }
    }

    public List<Class> classQueryByName(String className) {
        dbUtil = new DbUtil();
        Connection conn = dbUtil.getConnection();
        String sql = "select * from class where class_name like '%"+className+"%'";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            Class classes= new Class();
            List<Class> list = new ArrayList<>();
            while (rs.next()) {

                classes.setClassId(rs.getInt("class_id"));
                classes.setClassName(rs.getString("class_name"));
                classes.setGroupCount(rs.getInt("group_count"));
                list.add(classes);
            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
            return null;

        }finally {
            dbUtil.close(conn);//关闭链接
        }
    }
}
